﻿var pageIndex = 1;
var pageSize = 20;
var page_count = 0;

$.ajaxSetup({ url: "Proxy.ashx", type: "post", dataType: "json" });

// 绑定城区列表
function BindEmployee() {
    $.ajax({
        data: { method: "listEmployee" }
        , success: function (json) {
            $("#ddl_EmpNo").append("#full_EmpNo", json.listEmployee);

        }
    });
}

// 绑定城区列表
function BindInspectionRecordType () {
    $.ajax({
        data: { method: "listInspectionRecordType" }
        , success: function (json) {
            $("#ddl_Type").append("#full_Type", json.listType);

        }
    });
}

function btnSearch_Click() {
    GetListInspectionRecords();    
}

function GetListInspectionRecords() {
    var cityId = $("ddl_City").val();
    var areaId = $("ddl_Area").val();
    var empId = $("ddl_EmpNo").val();
    var typeId = $("ddl_Type").val();
    var startDate = $("txtBeginDate").val();
    var endDate = $("txtEndDate").val();

    $.ajax({
        data: { method: "listInspectionRecords",
            cityId: cityId,
            areaId: areaId,
            empId: empId,
            typeId: typeId,
            startDate: startDate,
            endDate: endDate,
            page: pageIndex,
            size: pageSize
        }
        , success: function (json) {

           // alert(JSON.stringify(json));
         

            // 先删除原来的行，否则会多次绑定。
            $("#tbl_InspectionRecords tr").each(function (trindex, tritem) {
                if (trindex != 0)
                    $(tritem).remove();
            });
            // 填充表格
            $("#tbl_InspectionRecords").append("#full_InspectionRecords", json.ListInspectionRecords);

            // 分页
            page_count = Math.ceil(json.RowsCount / pageSize);

            PagerInit();
        }
    });
}

function PageClick(pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: page_count, buttonClickCallback: PageClick });
    pageIndex = pageclickednumber;
    btnSearch_Click();
}

function PagerInit() {
    $(document).ready(function () {
        $("#pager").pager({ pagenumber: pageIndex, pagecount: page_count, buttonClickCallback: PageClick });
    });
}

var start = "", end = "";
function init() {
    BindCity();
    BindEmployee();
    BindInspectionRecordType();

    $("#txtBeginDate").datetimepicker({ dateFormat: "yy-mm-dd", onClose: function () { start = $("#txtBeginDate").datetimepicker("getDate"); } });
    $("#txtEndDate").datetimepicker({ dateFormat: "yy-mm-dd", onClose: function () { end = $("#txtEndDate").datetimepicker("getDate"); } });

    $("#txtBeginDate").datetimepicker("setDate", start);
    $("#txtEndDate").datetimepicker("setDate", end);

    $("#btnSearch").click(function () {
        btnSearch_Click();
    });

  // $(".pagediv").InitPage(Math.ceil(page_count / pageSize), pageIndex, btnSearch_Click);
}
